package com.example.chcrm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.chcrm.entity.common.ResponseMsg;
import com.example.chcrm.entity.common.TArea;
import com.example.chcrm.entity.employee.RoleModels;
import com.example.chcrm.service.TAreaService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author 邹
 * @Date 2025/4/23 0:56
 * @Version 1.0
 */
@RestController
@RequestMapping("/sys/area")
@CrossOrigin // 允许跨域访问
public class TAreaController {
    @Autowired
    private TAreaService tAreaService;

    /**
     * 分页查询行政区域
     *
     * @param tArea
     * @return
     */
    @PostMapping("/pageList")
    public ResponseMsg<IPage<TArea>> pageList(@RequestBody TArea tArea) {
//        ResponseMsg<Object> msg = new ResponseMsg<>();
//        Page<TArea> result = tAreaService.pageList(tArea);
//        msg.setData(result);
//        msg.success("查询成功！");
        return tAreaService.pageList(tArea);
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    public ResponseMsg<Object> add(@RequestBody TArea roleModels) {

        return  tAreaService.add(roleModels);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public ResponseMsg<Object> update(@RequestBody TArea roleModels) {
        return  tAreaService.update(roleModels);
    }

    @GetMapping("/delete/{id}")
    public ResponseMsg<Object> delete(@PathVariable String id) {
        ResponseMsg<Object> msg = new ResponseMsg<>();
        if (id == null || id.equals("")) {
            msg.fail("删除失败ID不能为空");
            return msg;
        }
        return tAreaService.delete(id);
    }

    @GetMapping("/getSecondLevelArea/{level}")
    public ResponseMsg<Object> getSecondLevelArea(@PathVariable String level){
        ResponseMsg<Object> msg = new ResponseMsg<Object>();
        if (!StringUtils.isNotBlank(level)){
            msg.fail("区域类型不能为空");
            return msg;
        }
        List<Map<String, Object>> areaList = tAreaService.getSecondLevelArea(level);
        msg.setData(areaList);
        msg.success("查询成功");
        return msg;

    }
}
